<?php
namespace Home\Model;
use Think\Model;

class CommodityInfoModel extends CommonModel {
	protected $pk     = 'commodity_id';
	
	var $info = array();
	
	function ini(){
		$sql = "select cpii.brief_pic,ci.* from ar_shop.commodity_info as ci
				left join (select * from ar_shop.commodity_pic_info as cpi where cpi.type=1) as cpii on cpii.commodity_id = ci.commodity_id
				where ci.is_cancle=0 order by ci.sort desc";
		$db_res = $this->query($sql);
		$db_res = $this->query($sql);
		if(empty($db_res)){$db_res = array();}
		foreach ($db_res as $row){
			$this->info[$row['commodity_id']]['commodity_id'] = $row['commodity_id'];//商品id
			$this->info[$row['commodity_id']]['name'] = $row['name'];//商品名称
			$this->info[$row['commodity_id']]['sale_cycle'] = $row['sale_cycle'];//销售周期
			$this->info[$row['commodity_id']]['spec'] = $row['spec'];//商品规格
			$this->info[$row['commodity_id']]['origin'] = $row['origin'];//产地
			$this->info[$row['commodity_id']]['unit_id'] = $row['unit_id'];//商品单位id
			$this->info[$row['commodity_id']]['market_price'] = num_f($row['market_price']);//市场价
			$this->info[$row['commodity_id']]['sale_price'] = num_f($row['sale_price']);//销售价格
			$this->info[$row['commodity_id']]['brief_pic'] = C('HTTP_IMG') .$row['brief_pic'];//商品封面小图
			$this->info[$row['commodity_id']]['is_onsale'] = !!$row['is_onsale'];
			$this->info[$row['commodity_id']]['supply_begin'] = $row['supply_begin'];//供应起始日
			$this->info[$row['commodity_id']]['supply_end'] = $row['supply_end'];//供应截止日
			$this->info[$row['commodity_id']]['brief_desc'] = $row['brief_desc'];//简述
			
		}
	}
	
	function commodity_info($id = null)
	{
		if(empty($id)){
			return $this->info;
		}else if(empty($this->info[$id])) {
			return array();
		}else{
			return $this->info[$id];
		}
	}
	
	//命中最近一个销售周期 //w6|14:00:00|172800
	function hitLastSaleCycle($id){
		$cinfo = $this->info[$id];
		// dump($cinfo['supply_begin']);
		// dump($cinfo['supply_end']);
		$sale_cycle = $this->info[$id]['sale_cycle'];
		$sale_cycle_arr = explode(',',trim($sale_cycle));
		$new_arr = array();
		foreach($sale_cycle_arr as $cycle){
			$cycle_arr = explode('|',$cycle);
			$collect_week = str_replace("w","",$cycle_arr[0]);
			if($collect_week-date("w")>=0){
				$collect_datetime = date("Y-m-d",strtotime("+".($collect_week-date("w"))." day"))." ".$cycle_arr[1];
				if($collect_datetime<=date("Y-m-d H:i:s")){
					$collect_datetime = date("Y-m-d H:i:s",strtotime($collect_datetime)+60*60*24*7);
				}
			}else{
				$collect_datetime = date("Y-m-d",strtotime("+".($collect_week-date("w")+7)." day"))." ".$cycle_arr[1];
			}
			if(!empty($cinfo['supply_begin']) && !empty($cinfo['supply_end'])){
				for($i=0;$i<5;$i++){
					$tmp = date("Y-m-d H:i:s",strtotime($collect_datetime)+60*60*24*7*$i);
					if($tmp>=$cinfo['supply_begin']. " 00:00:00" && $tmp<=$cinfo['supply_end']. " 24:00:00"){
						$collect_datetime = $tmp;
						break;
					}
				}
			}
			$dispatch_datetime = date("Y-m-d H:i:s",strtotime($collect_datetime)+$cycle_arr[2]);
			$new_arr[] = array('collect_datetime'=>$collect_datetime,'dispatch_datetime'=>$dispatch_datetime);
		}
		$new_arr = array_sort($new_arr,'collect_datetime','asc');
		return $new_arr[0];
	}
	
	//获取一周左右的销售周期
	function hitSaleCycleInWeek($id){
		$cinfo = $this->info[$id];
		$sale_cycle = $this->info[$id]['sale_cycle'];
		$sale_cycle_arr = explode(',',trim($sale_cycle));
		$new_arr = array();
		foreach($sale_cycle_arr as $cycle){
			$cycle_arr = explode('|',$cycle);
			$collect_week = str_replace("w","",$cycle_arr[0]);
			if($collect_week-date("w")>=0){
				$collect_datetime = date("Y-m-d",strtotime("+".($collect_week-date("w"))." day"))." ".$cycle_arr[1];
				if($collect_datetime<=date("Y-m-d H:i:s")){
					$collect_datetime = date("Y-m-d H:i:s",strtotime($collect_datetime)+60*60*24*7);
				}
			}else{
				$collect_datetime = date("Y-m-d",strtotime("+".($collect_week-date("w")+7)." day"))." ".$cycle_arr[1];
			}
			if(!empty($cinfo['supply_begin']) && !empty($cinfo['supply_end'])){
				for($i=0;$i<5;$i++){
					$tmp = date("Y-m-d H:i:s",strtotime($collect_datetime)+60*60*24*7*$i);
					if($tmp>=$cinfo['supply_begin']. " 00:00:00" && $tmp<=$cinfo['supply_end']. " 24:00:00"){
						$collect_datetime = $tmp;
						break;
					}
				}
			}
			$dispatch_datetime = date("Y-m-d H:i:s",strtotime($collect_datetime)+$cycle_arr[2]);
			$new_arr[] = array('collect_datetime'=>$collect_datetime,'dispatch_datetime'=>$dispatch_datetime);
		}
		$new_arr = array_sort($new_arr,'collect_datetime','asc');
		return $new_arr;
	}
}
?>